package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.res.ResWalletEntity;
import com.taomu.app.zongchouhuli.moudle.mine.entity.WalletListEntity;
import com.taomu.app.zongchouhuli.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class WalletListAdp extends RecyclerView.Adapter<WalletListAdp.WalletListHolder> {

    private List<ResWalletEntity.ListBean> mData;

    private Context mContext;

    private LayoutInflater inflater;
    //6(总收入) 1（支出列表） 3（充值） 4（提现）
    private String type;

    public WalletListAdp(Context mContext){
        this.mData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
    }

    public void setType(String type){
        this.type = type;
    }

    public void setData(List<ResWalletEntity.ListBean> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<ResWalletEntity.ListBean> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public WalletListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_wallet_list,parent,false);
        return new WalletListHolder(view);
    }

    @Override
    public void onBindViewHolder(WalletListHolder holder, int position) {
        final ResWalletEntity.ListBean entity = mData.get(position);
        holder.update(entity);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class WalletListHolder extends RecyclerView.ViewHolder{

        private TextView mTitleTv;

        private TextView mPriceTv;

        private TextView mTimeTv;

        private TextView mTypeTv;

        public WalletListHolder(View itemView) {
            super(itemView);
            mTitleTv = itemView.findViewById(R.id.item_wallet_title_tv);
            mPriceTv = itemView.findViewById(R.id.item_wallet_price_tv);
            mTimeTv = itemView.findViewById(R.id.item_wallet_time_tv);
            mTypeTv = itemView.findViewById(R.id.item_wallet_type_tv);
        }

        public void update(ResWalletEntity.ListBean entity){
            if("6".equals(type)) {
                if(TextUtils.isEmpty(entity.getAccount()) || "null".equals(entity.getAccount())){
                    mPriceTv.setText("¥0");
                }else {
                    mPriceTv.setText("¥" + entity.getAccount());
                }
                mTitleTv.setText(entity.getName());
            }
            if("1".equals(type)){
                if(TextUtils.isEmpty(entity.getAccount()) || "null".equals(entity.getAccount())){
                    mPriceTv.setText("¥0");
                }else {
                    mPriceTv.setText("¥" + entity.getAccount());
                }
                if("8".equals(entity.getAccount_rule_id())){
                    mTitleTv.setText(entity.getProduct_name());
                }else {
                    mTitleTv.setText(entity.getProduct_name() + "买入");
                }
            }
            if("3".equals(type)){
                mTitleTv.setText("充值");
                mPriceTv.setText("¥"+entity.getTotal_fee());
                if("recharge_wait_confirm".equals(entity.getRecharge_status_id())){
                    mTypeTv.setText("审核中");
                }else if("recharge_cancel".equals(entity.getRecharge_status_id())){
                    mTypeTv.setText("充值取消");
                }else if("recharge_complete".equals(entity.getRecharge_status_id())){
                    mTypeTv.setText("充值成功");
                }else if("recharge_fail".equals(entity.getRecharge_status_id())){
                    mTypeTv.setText("充值失败");
                }else if("recharge_wait_paid".equals(entity.getRecharge_status_id())){
                    mTypeTv.setText("待支付");
                }
            }
            if("4".equals(type)){
                mTitleTv.setText("提现");
                mPriceTv.setText(entity.getTotal_fee());
                if("forward_wait_confirm".equals(entity.getForward_status_id())){
                    mTypeTv.setText("审核中");
                }else if("forward_cancel".equals(entity.getForward_status_id())){
                    mTypeTv.setText("提现取消");
                }else if("forward_complete".equals(entity.getForward_status_id())){
                    mTypeTv.setText("提现成功");
                }else if("forward_fail".equals(entity.getForward_status_id())){
                    mTypeTv.setText("提现失败");
                }else if("forward_wait".equals(entity.getForward_status_id())){
                    mTypeTv.setText("审核中");
                }
            }
            if(!TextUtils.isEmpty(entity.getCreate_date())) {
                mTimeTv.setText(TimeUtils.timeFomart(Long.valueOf(entity.getCreate_date())));
            }

            //mTypeTv.setText(entity.getType());
        }
    }

    private WalletListAdpListener listener;

    public void setWalletListAdpListener(WalletListAdpListener listener){
        this.listener = listener;
    }

    public interface WalletListAdpListener{
        void onItemClick(ResWalletEntity.ListBean entity);
    }
}
